/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2018
 */

package com.bm.console.action;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.bm.console.business.WebContinentService;
import com.bm.console.business.WebCountryService;
import com.bm.console.pojo.WebCityItem;
import com.bm.console.pojo.WebContinent;
import com.bm.console.pojo.WebCountry;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bm.console.business.WebCityService;
import com.bm.console.pojo.WebCity;
import com.sys.object.common.ObjException;
import com.sys.object.common.ObjResultCode;
import com.sys.object.common.ObjResultException;
import com.sys.object.common.ObjResultJSON;
import com.sys.object.common.page.PageBean;

@Controller
public class WebCityAction {

    @Resource
    WebCityService webCityService;
    @Resource
    WebCountryService webCountryService;
    @Resource
    WebContinentService webContinentService;

    @RequestMapping(value = "/webCityList.do")
    public String webCityList(PageBean pageBean, Model model, WebCity webCity) {
        try {
            pageBean = webCityService.queryPageList(webCity, pageBean);
            model.addAttribute("pageBean", pageBean);
            model.addAttribute("startRow", pageBean.getStartRow());
            model.addAttribute("entity", webCity);

            List<WebCountry> webCountries = webCountryService.selectByAll();
            model.addAttribute("countries", webCountries);


            List<WebContinent> webContinents = webContinentService.selectByAll();
            model.addAttribute("continents", webContinents);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "/web/webcity/list";
    }

    /**
     * 新增动态 Get
     */
    @RequestMapping(value = "/addWebCity.do", method = RequestMethod.GET)
    public String addWebCity(@ModelAttribute("webCity") WebCity webCity, Model model, String id) {
        Integer prow = 0;
        try {
            if (StringUtils.hasText(id)) {
                WebCity record = webCityService.queryByPrimaryKey(Integer.parseInt(id));
                model.addAttribute("entity", record);

                List<WebCountry> webCountries = webCountryService.selectByAll();
                model.addAttribute("countries", webCountries);

                List<WebContinent> webContinents = webContinentService.selectByAll();
                model.addAttribute("continents", webContinents);

            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("prow", prow);
        return "/web/webcity/add";
    }

    /**
     * 新增动态 Post
     */
    @RequestMapping(value = "/saveWebCity.do", method = RequestMethod.POST)
    public String saveWebCity(@Validated @ModelAttribute("webCity") WebCity webCity,
                              BindingResult result, Model model, Integer prow,
                              HttpServletRequest request) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webCityService.saveOrUpdate(webCity);
            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {

            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);

            }
            e.printStackTrace();
            model.addAttribute("entity", webCity);
            model.addAttribute("ret", ret);
            return "/web/webcity/add";
        }
        return "redirect:/webCityList.do?status=";
    }

    /**
     * 新增动态 Post
     */
    @RequestMapping(value = "/saveWebCityJson.do", method = RequestMethod.POST)
    @ResponseBody
    public ObjResultException<String> saveWebCityJson(@RequestBody WebCity webCity, Model model,
                                                      HttpServletRequest request) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webCityService.saveOrUpdate(webCity);
            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }
        }
        return ret;
    }

    /**
     * 删除APP
     */
    @RequestMapping("/deleteWebCity.do")
    @ResponseBody
    public ObjResultException<String> deleteWebCity(Integer id) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webCityService.delete(id);

            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 修改状态APP
     */
    @RequestMapping("/updateStatusWebCity.do")
    @ResponseBody
    public ObjResultException<String> updateStatusWebCity(WebCity webCity) {
        ObjResultException<String> ret = new ObjResultException<String>();
        try {
            int success = webCityService.updateStatus(webCity);

            if (success > 0) {
                ret.setResultCode(ObjResultCode.OK);
            } else {
                ret.setResultCode(ObjResultCode.USER_LOGIN_ERROR);
            }
        } catch (Exception e) {
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }
        }
        return ret;
    }


    /**
     * 城市列表
     */
    @RequestMapping(value = "/cities.do", method = RequestMethod.GET)
    @ResponseBody
    public Object cities(Long countryId) {
        ObjResultJSON ret = new ObjResultJSON();
        try {
            List<WebCityItem> webCityItems = webCityService.selectByCountryId(countryId);
            ret.setResultCode(ObjResultCode.OK);
            ret.setData(webCityItems);
        } catch (Exception e) {
            if (e.getClass().equals(ObjException.class)) {
                ret.setResultCode(((ObjException) e).getCode());
            } else {
                ret.setResultCode(ObjResultCode.ERROR);
            }
        }
        return ret;
    }
}